import { Component } from '@angular/core';
import { NavController,NavParams,ActionSheetController,AlertController,ToastController } from 'ionic-angular';

import { NativeService } from '../../providers/NativeService';
import { EditorService } from '../../providers/EditorService';
import { ApiService } from '../../providers/ApiService';
import { Storage } from '@ionic/storage';


@Component({
  selector: 'page-createtask',
  templateUrl: 'createtask.html'
})
export class CreateTask {
  alltype = []
  allgrade = [
    {title:'紧急' },
    {title:'高' },
    {title:'中' },
    {title:'低' }
  ]
  token;
  receiveuser = []
  allcheckman = []
  form_content = {
    token:'',
    title: '',
    content:'',
    receiveuserid:'',
    type:'',
    tasktype:'',
    level:'',
    taskid: ''
  }
  taskid1 = '';
  dateMin;
  dateMax;
  flag = false;
  projecttype = [];
  constructor(
    public navCtrl: NavController,
    public alertCtrl: AlertController,
    private NativeService: NativeService,
    private EditorService: EditorService,
    private apiService: ApiService,
    public storage: Storage,
    private navParams: NavParams,
    public toastCtrl: ToastController,
    public actionSheetCtrl: ActionSheetController
  ){
    this.taskid1 = navParams.get('taskid');
    this.dateMin = this.formartDate(new Date());
    this.dateMax = this.formartDate(new Date(2100, 1, 1));
    //this.form_content.end_time = this.dateMin
    if(this.taskid1){
      this.flag = true;
    }else{
      this.flag = false;
    }
    console.log(this.flag)
  }
  ionViewDidEnter(){
    this.storage.get('mytoken').then((mytoken)=>{
      if(mytoken) {
        this.token = mytoken;
        this.form_content.token = mytoken;
      }
      this.getUser();
      this.getProjectType();
      this.getTaskType();
      this.getTaskDetail();
    })
    this.storage.get('formcontent').then((formcontent)=>{
      if(formcontent) {
        this.form_content = formcontent;
        this.form_content.content = '';
      }
    })
  }
  private dismissHandler() {
    //console.info('Toast onDidDismiss()');
  }
  //获取任务详情
  getTaskDetail(){
    this.apiService.getTaskDetails({token:this.form_content.token,taskid:this.taskid1},response => {
      if(response.json().status == 1){
        this.form_content = response.json().content.task;
        this.form_content.token = this.token;
      }
    })
  }
  //消息提示函数
  formTip(hint){
    const toast = this.toastCtrl.create({
      message: hint,
      duration: 3000,
      position: 'middle'
    });
    toast.onDidDismiss(this.dismissHandler);
    toast.present();
    return;
  }
  //获取用户列表
  getUser(){
    this.apiService.getUsers({token:this.form_content.token},response => {
      if(response.json().status== 1){
        this.receiveuser = response.json().content;
      }
    })
  }
  //获取任务类型
  getTaskType(){
    this.apiService.getTaskTypes({token:this.form_content.token},response => {
      if(response.json().status== 1){
        this.alltype = response.json().content;
      }
    })
  }
  //获取项目类型
  getProjectType(){
    this.apiService.getProjectTypes({token:this.form_content.token},response => {
      if(response.json().status== 1){
        this.projecttype = response.json().content;
      }
    })
  }
  //提交
  submitit(){
    this.addTask();
  }
  saveSelection(){
    this.EditorService.saveSelection();
  }
  restoreSelection(){
    this.EditorService.restoreSelection();
  }

  clickUploadImgBtn(){
    this.EditorService.clickUploadImgBtn(this.form_content.token);
  }

  formartDate (nowDate, status = true) {
    let y = nowDate.getFullYear();
    let m = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
    let d = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
    let h = nowDate.getHours();
    let i = nowDate.getMinutes();
    let s = nowDate.getSeconds();
    if (status) {
      return y + "-" + m + "-" + d
    }
    return y + "-" + m + "-" + d + " " + h + ":" + m + ':' + s
  }
  addTask(){
    let editableContent = document.getElementById("editor");
    this.form_content.content = editableContent.innerHTML;
    this.apiService.addTasks(this.form_content,response => {
      if(response.json().status== 1){
        this.formTip(response.json().info);
        this.form_content.title = '';
        this.storage.set('formcontent', this.form_content)
        this.navCtrl.pop()
      }else{
        this.formTip(response.json().info)
        return;
      }
    })
  }
  gomodifyTask(){
    let editableContent = document.getElementById("editor");
    this.form_content.content = editableContent.innerHTML;
    this.form_content.taskid = this.taskid1;
    console.log(this.form_content.taskid)
    this.apiService.taskModify(this.form_content,response => {
      if(response.json().status== 1){
        this.formTip(response.json().info);
        this.form_content.title = '';
        this.storage.set('formcontent', this.form_content)
        this.navCtrl.pop()
      }else{
        this.formTip(response.json().info)
        return;
      }
    })
  }

}
